package com.corechina.apex_osswords.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.corechina.apex_osswords.common.Response;
import com.corechina.apex_osswords.domain.entity.SysCompany;
import com.corechina.apex_osswords.domain.dao.SysCompanyMapper;
import com.corechina.apex_osswords.service.ISysCompanyService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;

/**
 * <p>
 * 公司配置表 服务实现类
 * </p>
 *
 * @author cc
 * @since 2022-03-11
 */
@Service
public class SysCompanyServiceImpl extends ServiceImpl<SysCompanyMapper, SysCompany> implements ISysCompanyService {

    @Autowired
    private SysCompanyMapper sysCompanyMapper;

    @Override
    public Response add(SysCompany sysCompany) {
        if (sysCompanyMapper.selectCount(new QueryWrapper<SysCompany>().eq("name", sysCompany.getName())) > 0) {
            return Response.error("该公司已存在，请勿重复添加");
        }
        sysCompanyMapper.insert(sysCompany);
        return Response.ok();
    }

    @Override
    public Response update(SysCompany sysCompany) {
        SysCompany sysCompany1 = sysCompanyMapper.selectOne(new QueryWrapper<SysCompany>().eq("name", sysCompany.getName()));
        if (sysCompany1 != null && !sysCompany1.getId().equals(sysCompany.getId())) {
            return Response.error("该公司已存在，请勿重复添加");
        }
        sysCompanyMapper.updateById(sysCompany);
        return Response.ok();
    }
}
